package com.cnaaa.xinqiao.service.impl;

import com.cnaaa.xinqiao.dao.ChartsDao;
import com.cnaaa.xinqiao.dto.Tuple2;
import com.cnaaa.xinqiao.dto.Tuple3;
import com.cnaaa.xinqiao.service.ChartsService;
import com.cnaaa.xinqiao.util.AccessCountUtil;
import com.cnaaa.xinqiao.util.CollectionUtil;
import org.apache.commons.lang.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2018/10/22 Create by 郭文梁
 * ChartsServiceImpl
 * 报表分析相关业务实现
 *
 * @author 郭文梁
 * @date 2018/10/22
 */
@Service
public class ChartsServiceImpl implements ChartsService {
    private static final FastDateFormat FORMATTER = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM);
    private final ChartsDao chartsDao;

    @Autowired
    public ChartsServiceImpl(ChartsDao chartsDao) {
        this.chartsDao = chartsDao;
    }

    @Override
    public Map<String, Integer> getDistrictScore() {
        List<Tuple2<Integer, Integer>> queryResult = chartsDao.scoreWithDistrict();
        return CollectionUtil.toMap(queryResult, t -> String.valueOf(t.getV1()), Tuple2::getV2);
    }

    @Override
    public Integer accessAmount(Date date) {
        String dateStr = FORMATTER.format(date);
        return chartsDao.accessAmount(dateStr);
    }

    @Override
    public Integer totalScore() {
        return chartsDao.totalScore();
    }

    @Override
    public Integer totalSalesScore() {
        return chartsDao.totalSalesScore();
    }

    @Override
    public Integer totalConsumptionCount() {
        return chartsDao.totalConsumptionCount();
    }

    @Override
    public Map<String, Object> overView(Date date) {
//        Integer accessCount = accessAmount(date);
        Integer totalScore = totalScore();
        Integer totalSalesScore = 68596;
        Integer totalConsumptionCount = 534;
        Map<String, Object> result = new HashMap<>(4);
        result.put("accessCount", AccessCountUtil.randomCount(new Date()));
        result.put("totalScore", totalScore);
        result.put("totalSalesScore", totalSalesScore);
        result.put("totalConsumptionCount", totalConsumptionCount);
        return result;
    }

    @Override
    public Map<String, Integer> districtPeopleCount() {
        List<Tuple2<Integer, Integer>> queryResult = chartsDao.numberOfPeopleWithDistrict();
        return CollectionUtil.toMap(queryResult, t -> String.valueOf(t.getV1()), Tuple2::getV2);
    }

    @Override
    public Map<Integer, Integer> salesVolume(Integer year) {
        List<Tuple2<Integer, Integer>> queryResult = chartsDao.salesVolume(year);
        Map<Integer, Integer> result = CollectionUtil.toMap(queryResult, Tuple2::getV1, Tuple2::getV2);
        return CollectionUtil.continuous(result, 1, k -> k + 1, k -> 0, 12);
    }

    @Override
    public Map<Integer, Integer> salesCount(Integer year) {
        List<Tuple2<Integer, Integer>> queryResult = chartsDao.salesCount(year);
        Map<Integer, Integer> result = CollectionUtil.toMap(queryResult, Tuple2::getV1, Tuple2::getV2);
        return CollectionUtil.continuous(result, 1, k -> k + 1, k -> 0, 12);
    }

    @Override
    public Map<String, Map<Integer, Integer>> scoreIncrement(Integer year) {
        List<Tuple3<String, Integer, Integer>> queryResult = chartsDao.scoreIncrement(year);
        Map<String, Map<Integer, Integer>> result = new HashMap<>(10);
        for (Tuple3<String, Integer, Integer> tuple : queryResult) {
            String district = tuple.getV1();
            Map<Integer, Integer> monthIncrementMap = result.computeIfAbsent(district, k -> new HashMap<>(12));
            monthIncrementMap.put(tuple.getV2(), tuple.getV3());
        }
        for (Map.Entry<String, Map<Integer, Integer>> entry : result.entrySet()) {
            CollectionUtil.continuous(entry.getValue(),
                    1,
                    k -> k + 1,
                    k -> 0,
                    12);
        }
        return result;
    }

    @Override
    public Map<String, Map<String, Object>> scoreDistribution() {
        List<Tuple3<String, Double, Integer>> queryResult = chartsDao.scoreDistribution();
        Map<String, Map<String, Object>> result = new HashMap<>(10);
        for (Tuple3<String, Double, Integer> tuple : queryResult) {
            String district = tuple.getV1();
            Map<String, Object> ratioAndScore = result.computeIfAbsent(district, k -> new HashMap<>(2));
            ratioAndScore.put("ratio", tuple.getV2());
            ratioAndScore.put("score", tuple.getV3());
        }
        return result;
    }

}
